/****************************************************
File Name:  qsort.cpp
Author:     Animesh Sharma
Date:       11/2005
Description:    Source code for Quicksort Algorithm - Array based Implementation(Project 3 cs352)
Submission Date: 1st Dec 2005
Project Due Date: 1st Dec 2005
****************************************************/


#include <iostream>  
#include <cstring>   
#include <fstream>
#include <ctype.h>
using namespace std;

int main(int argc,char *argv[])
{
	if (argc!=2) {
		cout << "usage: qsort file.txt" << endl;
		exit(1);
	}
	ifstream input(argv[1]);
	if (input.fail()) {
		cout << "error: could not open file " << argv[1] << endl;
		exit(1);
	}
	int noOfWords=0,sizeOfWords=0,maxLength=0;    
    char word[500];
    while (input >> word) {
        noOfWords++;
        sizeOfWords=strlen(word)+1;
        if(sizeOfWords>maxLength){
           maxLength=sizeOfWords;
        }
    }
	input.close();
    cout << "Number of words = " << noOfWords << endl;
    cout << "Maxlength of words = " << maxLength << endl;

    char *arrayOfWords[noOfWords]; 
    char wordList[sizeOfWords];
    int n = 0;
	ifstream fileo(argv[1]);
    while (fileo >> wordList) {
        arrayOfWords[n] = new char[strlen(wordList)+1]; // allocate space
        strcpy(arrayOfWords[n], wordList);      // copy word to new space
        n++;
    }

    cout << "Number of words = " << n << endl;
    for (int i=0; i<n; i++) {
        cout << *arrayOfWords[i] << "chara" << i << "\t" << arrayOfWords[i] << endl; // print the word
        delete [] arrayOfWords[i];       // free space
        arrayOfWords[i] = NULL;          // remove pointer
    }
   	fileo.close();
	return(0);
}


